package com.quan.system.web.controller;

import java.util.Date;

import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.common.bean.ResultBean;
import com.quan.common.core.MyBaseController;
import com.quan.common.utils.DateUtils;

/**
 * <p>
 * 地区表 前端控制器
 * </p>
 *
 * @author 杨浩泉
 * @since 2019-06-05
 */
@RestController
@RequestMapping("/system/test")
public class SystemTestController extends MyBaseController {

	@Autowired
	private Scheduler scheduler;

	/**
	 * 新增一个定时作业
	 * 
	 * @return
	 */
	@GetMapping("/quartz/add")
	@SuppressWarnings("unchecked")
	public Object addJob() {
		try {
			logger.debug("添加任务并启动.......");

			Class<Job> clz = (Class<Job>) Class.forName("com.quan.system.common.job.TestJob");

			// 构建JOB信息
			JobDetail job = JobBuilder.newJob(clz).withIdentity("jobName-001", "jobGroup-001")
					.withDescription("jobDesc-001").build();

			// 触发时间点
			CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger-jobName-001", "jobGroup-001").startNow()
					.withSchedule(cronScheduleBuilder).build();

			// 交由Scheduler安排触发
			Date scheduleJob = scheduler.scheduleJob(job, trigger);
			System.out.println(scheduleJob);
			System.out.println(DateUtils.formatDateTime(scheduleJob));

		} catch (Exception e) {
			e.printStackTrace();
		}

		return ResultBean.ok();
	}

	/**
	 * 暂停一个定时作业
	 * 
	 * @return
	 */
	@GetMapping("/quartz/pause")
	public Object pauseJob() {
		try {
			logger.debug("暂停任务.......");
			
			JobKey key = new JobKey("jobName-001", "jobGroup-001");
			scheduler.pauseJob(key);
			
		} catch (SchedulerException e) {
			e.printStackTrace();
		}

		return ResultBean.ok();
	}

	/**
	 * 恢复一个定时作业
	 * 
	 * @return
	 */
	@GetMapping("/quartz/resume")
	public Object resumeJob() {
		try {
			logger.debug("恢复任务.......");
			
			JobKey key = new JobKey("jobName-001", "jobGroup-001");
			scheduler.resumeJob(key);
			
		} catch (SchedulerException e) {
			e.printStackTrace();
		}

		return ResultBean.ok();
	}

	/**
	 * 删除一个定时作业
	 * 
	 * @return
	 */
	@GetMapping("/quartz/delete")
	public Object deleteJob() {
		try {
			logger.debug("删除任务.......");
			
			//暂停任务
			JobKey key = new JobKey("jobName-001", "jobGroup-001");
			scheduler.pauseJob(key);
			
			TriggerKey triggerKey = TriggerKey.triggerKey("jobName-001", "jobGroup-001");  
            // 停止触发器  
            scheduler.pauseTrigger(triggerKey);  
            // 移除触发器  
            scheduler.unscheduleJob(triggerKey);  
            
            // 删除任务  
            scheduler.deleteJob(key);  
            System.out.println("triggerKey:"+triggerKey.toString());
            System.out.println("removeJob:"+key.toString());  
			
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
		return ResultBean.ok();
	}

}
